package com.study.dataStructure.searchAlgorithms;

/**
 * @author tiny
 * @date 2021/11/10 11:29
 * @Description: 顺序查找
 */
public class SequentialSearch {

    //最简单粗暴的
    public static int search(int[] arr,int key){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==key) return i;
        }
        return -1;
    }

    //改造版,免去了每次限制是否索引越界
    public static int search2(int[] arr,int key){
        if(arr[0] == key) return 0;
        arr[0] = key;

        int i = arr.length-1; //末位索引
        while(arr[i] == key){
            i--;
        }
        if(i==0) return -1;
        return i;
    }
}
